/*
 *   Copyright 1999-2016 Asiainfo Technologies(China),Inc.
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *
 */
package com.asiainfo.redis.scheduling;

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.util.ReflectionUtils;

import java.lang.reflect.Method;

/**
 * @author william.xu
 * @version $Id: MethodInvokingJob.java,v 1.1 2013/06/18 09:12:19 xuwz Exp $
 * Created on 2013-5-21 下午4:32:14
 */

public class MethodInvokingJob extends BaseJob{

	//父类QuartzJobBean通过BeanWrapper类的setPropertyValues注入以下属性
	protected String targetBeanName;
	protected String targetMethod;
	
	public void setTargetBeanName(String targetBeanName) {
		this.targetBeanName = targetBeanName;
	}

	public void setTargetMethod(String targetMethod) {
		this.targetMethod = targetMethod;
	}

	public void executeInternal(JobExecutionContext context) throws JobExecutionException {
		try {
			Object targetObject = this.applicationContext.getBean(targetBeanName);
			Class targetClass = targetObject.getClass();
			Method methodObject = targetClass.getMethod(targetMethod, new Class[0]);
			ReflectionUtils.invokeMethod(methodObject, targetObject);
		} catch (Exception e) {
			throw new JobExecutionException(e);
		}
	}

}
